package com.sskj.gcc.ui.fragment;


import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.base.App;
import com.sskj.common.box.imageloader.GlideImageLoader;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.BannerBean;
import com.sskj.gcc.bean.GuideListBean;
import com.sskj.gcc.bean.ServiceLinkBean;
import com.sskj.gcc.component.DaggerUserDataComponent;
import com.sskj.gcc.http.HttpConfig;
import com.sskj.gcc.presenter.HangqingFragmentPresenter;
import com.sskj.gcc.ui.activity.ChargeMoneyActivity;
import com.sskj.gcc.ui.activity.GuideActivity;
import com.sskj.gcc.ui.activity.KeFuWebViewActivity;
import com.sskj.gcc.ui.activity.WithdrawActivity;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.AseetListBean;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.mine.MyShareActivity;
import com.sskj.gocoin.mine.VerifyHomeActivity;
import com.sskj.gocoin.utils.NumberUtil1;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.CapitalDetailsActivity;
import com.sskj.gocoin.v.activity.ExchangeActivity;
import com.sskj.gocoin.v.activity.PayPwdSettingActivity;
import com.sskj.gocoin.v.activity.VerifyJuniorActivity;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.level.ui.activity.LevelActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.TipUtil;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;

import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.sskj.gocoin.common.Constants.VERIFY_IDENT_CODE;

@Route(path = RConfig.APP_HANGQING)
public class HangqingFragment extends BaseFragment<HangqingFragmentPresenter> {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tvNotice)
    TextSwitcher tvNotice;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.tvCoinTypeFirst)
    TextView tvCoinTypeFirst;
    @BindView(R.id.tvCoinUSDTFirst)
    TextView tvCoinUSDTFirst;
    @BindView(R.id.tvCoinRateFirst)
    TextView tvCoinRateFirst;
    @BindView(R.id.tvCoinCNYFirst)
    TextView tvCoinCNYFirst;
    @BindView(R.id.tvCoinTypeSecond)
    TextView tvCoinTypeSecond;
    @BindView(R.id.tvCoinUSDTSecond)
    TextView tvCoinUSDTSecond;
    @BindView(R.id.tvCoinRateSecond)
    TextView tvCoinRateSecond;
    @BindView(R.id.tvCoinCNYSecond)
    TextView tvCoinCNYSecond;
    @BindView(R.id.tvCoinTypeThird)
    TextView tvCoinTypeThird;
    @BindView(R.id.tvCoinUSDTThird)
    TextView tvCoinUSDTThird;
    @BindView(R.id.tvCoinRateThird)
    TextView tvCoinRateThird;
    @BindView(R.id.tvCoinCNYThird)
    TextView tvCoinCNYThird;
    @BindView(R.id.day_day_dig_mine)
    LinearLayout dayDayDigMine;
    @BindView(R.id.tvMore)
    TextView tvMore;
    @BindView(R.id.ll_hangqing_xszd)
    LinearLayout xszdLl;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;

    @BindView(R.id.asset_recyclerView)
    RecyclerView assetRecyclerView;
    @BindView(R.id.toolBarLayout)
    ToolBarLayout toolBarLayout;

    @BindView(R.id.tvChargeMoney)
    TextView tvChargeMoney;
    @BindView(R.id.tvWithdrawMoney)
    TextView tvWithdrawMoney;
    @BindView(R.id.tvExchange)
    TextView tvExchange;
    @BindView(R.id.tvAsset)
    TextView tvAsset;
    @BindView(R.id.tvAssetCny)
    TextView tvAssetCny;

    @BindView(R.id.llBtc)
    LinearLayout llBtc;
    @BindView(R.id.llBch)
    LinearLayout llBch;
    @BindView(R.id.llEos)
    LinearLayout llEos;
    @BindView(R.id.tvAboutTo)
    TextView tvAboutTo;
    @BindView(R.id.tvAssetCopy)
    TextView tvAssetCopy;
    @BindView(R.id.tvAssetCnyCopy)
    TextView tvAssetCnyCopy;
    @BindView(R.id.toolbar_rightbutton)
    TextView toolBarRightButton;

    private Disposable noticeDispo;
    private GuideListBean noticeList;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private ValueAnimator valueAnimator;
    private ValueAnimator valueAnimator1;
    private ValueAnimator valueAnimator2;
    private Disposable firstDispo;
    private Disposable secondDispo;
    private Disposable thirdDispo;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    DecimalFormat mFormat = new DecimalFormat("0.0000");
    private boolean isSee = true;
    private CoinBean coinBean1;
    private CoinBean coinBean2;
    private CoinBean coinBean3;
    private UserInfo userInfo;
    private ServiceLinkBean serviceLinkBean;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_hangqing;
    }

    @Override
    public HangqingFragmentPresenter getPresenter() {
        return new HangqingFragmentPresenter();
    }

    @Override
    public void initView() {
        LanguageUtil.changeAppLanguage(getActivity(), LanguageUtil.getAppLocale(App.INSTANCE), true);
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        toolBarLayout.setTitle("IM COIN");
        toolBarRightButton.setTextSize(12);
        toolBarLayout.setLeftButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RConfig.SLIDE_MAIN).navigation();
            }
        });
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mPresenter.getData();
                if (userData != null) {
                    mPresenter.requestUserAssetList(true);
                    mPresenter.getUserInfo();
                    mPresenter.getAsset();
                    mPresenter.serviceLink();
                }
            } else {
                userData = null;
            }
        });
        if (LanguageUtil.isChinese()) {
            mPresenter.getBanner("0");
        } else {
            mPresenter.getBanner("4");
        }
        mPresenter.getNotice();
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getData();
                if (userData != null) {
                    mPresenter.requestUserAssetList(true);
                    mPresenter.getUserInfo();
                    mPresenter.getAsset();
                }
            }
        });
        //更多公告
        ClickUtil.click(tvMore, () -> {
            ARouter.getInstance().build(RConfig.APP_GONGGAO_LIST).navigation();
        });
        //天天挖矿
        ClickUtil.click(dayDayDigMine, () -> {
            if (userData == null) {
                ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                return;
            }
            ARouter.getInstance().build(RConfig.APP_MINING).navigation();
        });
        //新手指导
        ClickUtil.click(xszdLl, () -> WebViewActivity.start(getActivity(), "xszd"));
        ClickUtil.click(200, ivPwdToggle, () -> {
            if (isSee) {
                isSee = false;
//                tvAsset.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                tvAssetCny.setTransformationMethod(PasswordTransformationMethod.getInstance());
                tvAsset.setVisibility(View.GONE);
                tvAssetCny.setVisibility(View.GONE);
                tvAboutTo.setVisibility(View.GONE);
                tvAssetCopy.setVisibility(View.VISIBLE);
                tvAssetCnyCopy.setVisibility(View.VISIBLE);
                ivPwdToggle.setImageResource(R.mipmap.app_icon_hide);
            } else {
                isSee = true;
//                tvAsset.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                tvAssetCny.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                tvAsset.setVisibility(View.VISIBLE);
                tvAssetCny.setVisibility(View.VISIBLE);
                tvAboutTo.setVisibility(View.VISIBLE);
                tvAssetCopy.setVisibility(View.GONE);
                tvAssetCnyCopy.setVisibility(View.GONE);
                ivPwdToggle.setImageResource(R.mipmap.app_icon_show);
            }
        });
        banner.setImageLoader(new GlideImageLoader()).setBannerAnimation(Transformer.Default);
        banner.setOffscreenPageLimit(1);
//        banner.setPageTransformer(false, new ZoomInTransformer());

        tvNotice.setFactory(() -> {
            TextView textView = new TextView(getActivity());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 26);
            textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            textView.setGravity(Gravity.CENTER_VERTICAL);
            textView.setSingleLine();
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.app_8e9));
            return textView;
        });

        assetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        assetRecyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 2))
                .setDividerColor(ContextCompat.getColor(getActivity(), R.color.app_divider)));
        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(com.sskj.gocoin.R.layout.item_asset_manage, null, assetRecyclerView) {
            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_type, item.pname);
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_available_balance, mFormat.format(item.usable));
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_frozen_capital, mFormat.format(item.frost));
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_cny, NumberUtil1.keepHalf(item.cny, 2));
//                holder.setOnClickListener(R.id.item_asset_manage_record, v -> {//账户明细
//                    startActivity(new Intent(getActivity(), CapitalDetailsActivity.class)
//                            .putExtra(Constants.INTENT_ASSET_ID, item.pid)
//                            .putExtra(Constants.COIN_TYPE, item.pname));
                ClickUtil.click(1000, holder.itemView, new ClickUtil.Click() {
                    @Override
                    public void click() {
                        startActivity(new Intent(getActivity(), CapitalDetailsActivity.class)
                                .putExtra(Constants.INTENT_ASSET_ID, item.pid)
                                .putExtra(Constants.COIN_TYPE, item.pname));
                    }
                });
                holder.setText(R.id.available_text, com.sskj.gocoin.common.App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaAssetManagerActivity2) + "(" + item.pname + ")");
                holder.setText(R.id.freeze_text, com.sskj.gocoin.common.App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaAssetManagerActivity3) + "(" + item.pname + ")");
            }
        };

        //充币
        ClickUtil.click(tvChargeMoney, new ClickUtil.Click() {
            @Override
            public void click() {
                if (userData == null) {
                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                    getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                    return;
                }
                //未绑定啥也不能干
                if (userData.getConfig().getGoogle_bind() == 0) {
                    TipUtil.googleBindTip(getActivity(), getString(R.string.lib_google_bind), () -> {
                        ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                    });
                } else {
                    ChargeMoneyActivity.start(getActivity());
                }
            }
        });
        //提币
        ClickUtil.click(tvWithdrawMoney, new ClickUtil.Click() {
            @Override
            public void click() {
                if (userData == null) {
                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                    getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                    return;
                }
                //未绑定啥也不能干
                if (userData.getConfig().getGoogle_bind() == 0) {
                    TipUtil.googleBindTip(getActivity(), getString(R.string.lib_google_bind), () -> {
                        ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                    });
                } else {
                    check();
                }
            }
        });
        //兑换
        ClickUtil.click(tvExchange, new ClickUtil.Click() {
            @Override
            public void click() {
                if (userData == null) {
                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                    getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                    return;
                }
                //未绑定啥也不能干
                if (userData.getConfig().getGoogle_bind() == 0) {
                    TipUtil.googleBindTip(getActivity(), getString(R.string.lib_google_bind), () -> {
                        ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                    });
                } else {
                    ExchangeActivity.start(getActivity());
                }
            }
        });
        ClickUtil.click(llBtc, new ClickUtil.Click() {
            @Override
            public void click() {
                if (coinBean1 != null) {
                    ARouter.getInstance().build(RConfig.APP_MARKET)
                            .withSerializable(Constans.CODE, coinBean1)
                            .navigation();
                }
            }
        });
        ClickUtil.click(llBch, new ClickUtil.Click() {
            @Override
            public void click() {
                if (coinBean2 != null) {
                    ARouter.getInstance().build(RConfig.APP_MARKET)
                            .withSerializable(Constans.CODE, coinBean2)
                            .navigation();
                }
            }
        });
        ClickUtil.click(llEos, new ClickUtil.Click() {
            @Override
            public void click() {
                if (coinBean3 != null) {
                    ARouter.getInstance().build(RConfig.APP_MARKET)
                            .withSerializable(Constans.CODE, coinBean3)
                            .navigation();
                }
            }
        });
        toolBarLayout.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (serviceLinkBean != null) {
                    if (!TextUtils.isEmpty(serviceLinkBean.getService_link())) {
                        KeFuWebViewActivity.start(getActivity(), serviceLinkBean.getService_link());
                    }
                }
            }
        });
    }

    private void check() {
        if (userInfo == null) {
            return;
        }
        if (checkIdent(getActivity())) {
            if (checkPwdStatus()) {
                if (checkIdentHign(getActivity())) {
                    WithdrawActivity.start(getActivity());
                }
            }
        }
    }

    public void onDataSuccess(AseetListBean data) {
        mAdapter.setNewData(data.asset);
    }

    /**
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshCoin(CoinBean data) {
        if (tvCoinCNYFirst == null || data == null)
            return;
        if (data.getCode().equals("btc_usdt")) {
            coinBean1 = data;
        } else if (data.getCode().equals("bch_usdt")) {
            coinBean2 = data;
        } else if (data.getCode().equals("eos_usdt")) {
            coinBean3 = data;
        }
        Flowable.fromArray(data)
                .filter(coinBean -> {
                    if (coinBean.getCode().toUpperCase().equals("BTC_USDT")) {
                        tvCoinUSDTFirst.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYFirst.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateFirst.setText(coinBean.getChangeRate());
                        tvCoinUSDTFirst.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        tvCoinRateFirst.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        DisposUtil.close(firstDispo);
                        firstDispo = Flowable.timer(1, TimeUnit.SECONDS)
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(aLong -> {
                                    if (tvCoinUSDTFirst == null)
                                        return;
//                                    tvCoinUSDTFirst.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.black));
                                });
                        return false;
                    }
                    return true;
                })
                .filter(coinBean -> {
                    if (coinBean.getCode().toUpperCase().equals("BCH_USDT")) {
                        tvCoinUSDTSecond.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYSecond.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateSecond.setText(coinBean.getChangeRate());
                        tvCoinUSDTSecond.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        tvCoinRateSecond.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        DisposUtil.close(secondDispo);
                        secondDispo = Flowable.timer(1, TimeUnit.SECONDS)
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(aLong -> {
                                    if (tvCoinUSDTSecond == null)
                                        return;
//                                    tvCoinUSDTSecond.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.black));
                                });
                        return false;
                    }
                    return true;
                })
                .filter(coinBean -> {
                    if (coinBean.getCode().toUpperCase().equals("EOS_USDT")) {
                        tvCoinUSDTThird.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYThird.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateThird.setText(coinBean.getChangeRate());
                        tvCoinUSDTThird.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        tvCoinRateThird.setTextColor(ContextCompat.getColor(App.INSTANCE, coinBean.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
                        DisposUtil.close(thirdDispo);
                        thirdDispo = Flowable.timer(1, TimeUnit.SECONDS)
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(aLong -> {
                                    if (tvCoinUSDTThird == null)
                                        return;
//                                    tvCoinUSDTThird.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.black));
                                });

                        return false;
                    }
                    return true;
                }).subscribe();

        ;
    }

    public void updateUI(List<CoinBean> data) {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        if (data == null || tvCoinCNYFirst == null) {
            return;
        }
        RxBus.getDefault().post(data);
        Flowable.fromIterable(data)
                .filter(coinBean -> {
                    if ("BTC_USDT".equals(coinBean.getCode().toUpperCase())) {
                        tvCoinUSDTFirst.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYFirst.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateFirst.setText(coinBean.getChangeRate());
                        return false;
                    }
                    return true;
                })
                .filter(coinBean -> {
                    if ("BCH_USDT".equals(coinBean.getCode().toUpperCase())) {
                        tvCoinUSDTSecond.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYSecond.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateSecond.setText(coinBean.getChangeRate());
                        return false;
                    }
                    return true;
                })
                .filter(coinBean -> {
                    if ("EOS_USDT".equals(coinBean.getCode().toUpperCase())) {
                        tvCoinUSDTThird.setText(NumberUtil.keep2(coinBean.getPrice()));
                        tvCoinCNYThird.setText(String.format("≈%s CNY", coinBean.getCnyPrice()));
                        tvCoinRateThird.setText(coinBean.getChangeRate());
                        return false;
                    }
                    return true;
                }).subscribe();

    }

    public void updateBanner(List<BannerBean> data) {
        if (banner == null)
            return;
        Flowable.fromIterable(data)
                .map(bannerBean -> HttpConfig.BASE_URL + bannerBean.getPath())
                .toList()
                .subscribe(strings -> {
                    banner.setImages(strings);
                    banner.setOnBannerListener(position -> {
//                        if (!TextUtils.isEmpty(data.get(position).getUrl())) {
////                            ARouter.getInstance().build(RConfig.APP_GUIDE_WEB)
////                                    .withString(Constans.URL, data.get(position).getUrl())
////                                    .navigation();
//                            BannerWebActivity.start(getActivity(), data.get(position).getUrl(), data.get(position).getName());
//
//                        }
                        if (!TextUtils.isEmpty(data.get(position).getLink_type())) {
                            if ("1".equals(data.get(position).getLink_type())) {   //个人推广
                                MyShareActivity.start(getActivity());
                            } else if ("2".equals(data.get(position).getLink_type())) {  //交易指南
                                GuideActivity.start(getActivity());
                            } else if ("3".equals(data.get(position).getLink_type())) {  //天天挖矿
                                ARouter.getInstance().build(RConfig.APP_MINING).navigation();
                            } else if ("4".equals(data.get(position).getLink_type())) {   //新手指导
                                WebViewActivity.start(getActivity(), "xszd");
                            } else if ("5".equals(data.get(position).getLink_type())) {   //合约交易
                                LevelActivity.start(getActivity(), "btc_usdt");
                            }
                        }
                    });
                    banner.start();
                });
    }


    @Override
    public void onDestroy() {
        DisposUtil.close(noticeDispo);
        DisposUtil.close(firstDispo);
        DisposUtil.close(secondDispo);
        DisposUtil.close(thirdDispo);
        super.onDestroy();
    }

    public void finishRefresh() {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
    }

    public void updateNotice(List<GuideListBean> notice) {
        if (tvNotice != null) {
            if (notice.size() > 0) {
                DisposUtil.close(noticeDispo);
                noticeDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
                        .onBackpressureDrop()
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(i -> {
                            if (tvNotice != null) {
                                noticeList = notice.get((int) (i % notice.size()));
                                tvNotice.setText(noticeList.getTitle());
                                ClickUtil.click(tvNotice, () -> {
                                    ARouter.getInstance()
                                            .build(RConfig.APP_NOTICE_DETAIL)
                                            .withString(Constans.HEAD, App.INSTANCE.getString(R.string.appJavaGuideListFragment8))
                                            .withString(Constans.ID, noticeList.getId())
                                            .withString(Constans.TIME, noticeList.getCreate_time())
                                            .withString(Constans.TITLE, noticeList.getTitle())
                                            .navigation();
                                });
                            }
                        }, System.out::println);
            }
        }
    }

    protected boolean checkIdent(Context context) {
        if (userInfo.getAuthentication() == 1) {//已实名认证
            return true;
        } else {
            showApplyTip(getContext(), App.INSTANCE.getString(R.string.strBaseActivity204), true);
            return false;
        }

    }

    /**
     * 判断用户高级实名认证  高级认证状态 1 未认证 2 待审核 3 已通过  4拒绝
     */
    protected boolean checkIdentHign(Context context) {
        if (userInfo.getAuthentication() == 3) {//已实名认证
            return true;
        } else if (userInfo.getAuthentication() == 2) {//审核中
            ToastUtil.showShort(App.INSTANCE.getString(R.string.strBaseActivity205));
//            VerifyHomeActivity.start(getContext());
            return false;
        } else if (userInfo.getAuthentication() == 4) {//被拒绝
//            VerifyHomeActivity.start(getContext());
            toValidate(context, App.INSTANCE.getString(R.string.strBaseActivity222));
            return false;
        } else {
            toValidate(context, App.INSTANCE.getString(R.string.strBaseActivity223));
//            VerifyHomeActivity.start(getContext());
            return false;
        }
    }

    private void toValidate(Context context, String content) {
        TipUtil.getTip(context, content, new TipUtil.OnTipSure() {
            @Override
            public void onSure() {
                VerifyHomeActivity.start(getContext());
            }
        }).show();
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(Context mcontext, String msg, boolean status) {
        AlertDialog tipDialog = new AlertDialog.Builder(mcontext)
                .setTitle(App.INSTANCE.getString(R.string.strBaseFragment210))
                .setMessage(msg)
                .setPositiveButton(status ? App.INSTANCE.getString(R.string.strBaseFragment211) : App.INSTANCE.getString(R.string.strBaseFragment212), (dialog, which) -> {
                    dialog.dismiss();
                    if (status) {
                        openActivity(VerifyJuniorActivity.class, VERIFY_IDENT_CODE);
                    }
                })
                .setNegativeButton(App.INSTANCE.getString(R.string.strBaseFragment213), (dialog, which) -> dialog.dismiss())
                .create();
        tipDialog.show();

    }

    protected void openActivity(Class<? extends Activity> clz, int requestCode) {
        if (MyAppLication.isLogin()) {
            startActivityForResult(new Intent(getActivity(), clz), requestCode);
        } else {
            ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
            getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);

        }
    }

    public boolean checkPwdStatus() {
        if (userData != null) {
            if (userInfo.getConfig().getPayment_password_set() == 1) {
                return true;
            } else {
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle(R.string.notice)
                        .setMessage(App.INSTANCE.getString(R.string.strTransferActivity206))
                        .setPositiveButton(getString(R.string.common_sure), (dialog1, which) -> {
                            dialog1.dismiss();
                            getActivity().startActivity(new Intent(getActivity(), PayPwdSettingActivity.class));
                        }).setNegativeButton(getString(R.string.common_cancel), (dialog12, which) -> dialog12.dismiss())
                        .show();

                return false;
            }
        } else {
            ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
        }
        return false;
    }

    /**
     * 总资产明细
     */
    public void refresh(AseetListBean asset) {
        if (asset != null) {
            tvAsset.setText(NumberUtil.keep4(asset.wallone));
            tvAssetCny.setText(NumberUtil.keep2(asset.walloneCny) + "CNY");
        }
    }

    public void updateUserInfo(UserInfo data) {
        this.userInfo = data;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userViewModel != null) {
            userViewModel.update();
        }
    }

    public void getServiceLink(ServiceLinkBean data) {
        this.serviceLinkBean = data;
    }
}
